package net.thelostlands.plugins.thelostfortress;

import com.palmergames.bukkit.towny.NotRegisteredException;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author fabio
 */
public class ListenToPlayer implements Listener {

    HashMap<String, Location> entering = new HashMap<String, Location>();

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent e) {
        if (Main.area.find(e.getTo())) {
            Player p = e.getPlayer();
            if (!Main.area.find(e.getFrom())) {
                if (Main.permissions.has(e.getPlayer(), "thelostfortress.admin") || Main.guerraAttiva || Main.construction) {
                    String m = "Sei dentro la fortezza ";
                    String f = Main.area.propertyOf(e.getTo());
                    m += f == null ? "!" : " di " + f;
                    p.sendMessage(ChatColor.GREEN + m);
                    entering.put(p.getName(), e.getFrom());
                    return;
                }
                p.sendMessage(ChatColor.RED + "Non puoi ancora entrare");
                p.teleport(e.getFrom(), TeleportCause.COMMAND);
                e.setCancelled(true);


            } else {
                if (Main.permissions.has(p, "thelostfortress.admin") || Main.guerraAttiva || Main.construction) {
                    return;
                }
                Block b = e.getFrom().getBlock();
                for (BlockFace bf : BlockFace.values()) {
                    Block relative = b.getRelative(bf);
                    if (!bf.equals(BlockFace.DOWN) && !bf.equals(BlockFace.UP) && !Main.area.find(relative.getLocation())) {
                        return;
                    }
                }
                Location l;
                if (entering.containsKey(p.getName())) {
                    l = entering.remove(p.getName());
                } else {
                    if (entering.isEmpty()) {
                        l = p.getWorld().getSpawnLocation();
                    } else {
                        l = entering.values().iterator().next();
                    }
                }

                p.teleport(l, TeleportCause.COMMAND);
                e.setCancelled(true);

            }
        } else if (Main.area.find(e.getFrom())) {
            if (!Main.area.find(e.getTo()) && Main.mondo.equals(e.getFrom().getWorld().getName())) {
                e.getPlayer().sendMessage(ChatColor.GREEN + "Hai lasciato la fortezza.");
            }
        }
    }

@EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (!Main.construction && e.hasBlock() && Main.area.find(e.getClickedBlock().getLocation())) {
            if (Main.guerraAttiva && e.hasBlock() 
                    && e.getClickedBlock().getType().equals(Material.CHEST) 
                    && Main.area.findChest(e.getClickedBlock().getLocation())) {
                String name = e.getPlayer().getName();
                boolean eremita = true;
                try {
                    name = Main.towny.getTownyUniverse().getResident(name).getTown().getName();
                    eremita = false;
                    name = Main.towny.getTownyUniverse().getTown(name).getNation().getName();

                } catch (NotRegisteredException ex) {
                }
                e.getPlayer().sendMessage(ChatColor.GREEN + "Hai aperto una cassa!!");
                int x = Main.area.openChest(e.getClickedBlock().getLocation(), name);
                if (!eremita) {
                    name = e.getPlayer().getName() + " (" + name + ")";
                }
                switch (x) {
                    case 1:
                        Main.one.getServer().broadcastMessage(ChatColor.DARK_RED + name + " ha conquistato una fortezza!");
                        break;
                    case 2:
                        Main.one.getServer().broadcastMessage(ChatColor.DARK_RED + name + " ha messo in pericolo una fortezza!");
                        break;
                }

            }
            else if(e.hasBlock() && e.getItem().getType().equals(Material.FLINT_AND_STEEL)){
                e.setCancelled(true);                
            }
        }
    }
    

}
